/*
 * @Date: 2023-03-06 13:52:19
 * @LastEditTime: 2023-03-11 16:18:33
 * @FilePath: /xing-qiu-da-zhan/src/hooks/changeLang.ts
 * 介绍:
 */
import { Locale } from "vant";
import enUS from "vant/es/locale/lang/en-US";
import zhCN from "vant/es/locale/lang/zh-CN";
import { i18n } from "@@/language";
import useSysStore from "@@/store/useSysStore";

export default function changeLang(langKey: string | undefined) {
  try {
    const _langKey = langKey as keyof typeof langEnum;
    const sysStore = useSysStore();
    langKey ??= "zh-CN";
    console.log(`切换语言${langKey}`);
    const { locale } = i18n.global;
    locale.value = langKey;
    const langEnum = {
      "en-US": {
        key: "en-US",
        raw: enUS,
        apiLang: "en",
      },
      "zh-CN": {
        key: "zh-CN",
        raw: zhCN,
        apiLang: "zh-CN",
      },
    };
    Locale.use(langEnum[_langKey].key, langEnum[_langKey].raw);
    sysStore.setting = { ...sysStore.setting, language: langKey };
    sysStore.reqHeader = {
      ...sysStore.reqHeader,
      language: langEnum[_langKey].apiLang,
    };
  } catch (error) {
    console.warn(`切换语言失败${langKey}`, error);
  }
}
